package br.ufrj.cos.bri.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;


public class MyXLST{
	
	public static final String LOCAL_TO_GLOBAL_XSL_LOCATION = "doc/XSLT/LocalToGlobal.xsl";
	public static final String GLOBAL_TO_LOCAL_XSL_LOCATION = "doc/XSLT/GlobalToLocal.xsl";
	 
	    
	    public static String transformerFromLocalToGlobalSchema(String xml){
	    	
	    	try{
	    		File xsltFile = new File(LOCAL_TO_GLOBAL_XSL_LOCATION);
	    		
	    		Source xmlSource =
	    			new StreamSource(new ByteArrayInputStream(xml.getBytes()));
	    		
	    		Source xsltSource =
	                new StreamSource(xsltFile);
	    		
	    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    		
	    		javax.xml.transform.Result result =
	                new javax.xml.transform.stream.StreamResult(baos);
	    		
	    		
		        // create an instance of TransformerFactory
		        TransformerFactory transFact =
		                TransformerFactory.newInstance();
	    		
		        Transformer trans =
	                transFact.newTransformer(xsltSource);
	 
		        trans.transform(xmlSource, result);
		        
		        return baos.toString();
	    		
	    	} catch(Exception e){e.printStackTrace();}
	    	
	    	return null;

	    }
	    
	    public static String transformerFromGlobalToLocalSchema(String xml){
	    	
	    	try{
	    		File xsltFile = new File(GLOBAL_TO_LOCAL_XSL_LOCATION);
	    		
	    		Source xmlSource =
	    			new StreamSource(new ByteArrayInputStream(xml.getBytes()));
	    		
	    		Source xsltSource =
	                new StreamSource(xsltFile);
	    		
	    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    		
	    		javax.xml.transform.Result result =
	                new javax.xml.transform.stream.StreamResult(baos);
	    		
	    		
		        // create an instance of TransformerFactory
		        TransformerFactory transFact =
		                TransformerFactory.newInstance();
	    		
		        Transformer trans =
	                transFact.newTransformer(xsltSource);
	 
		        trans.transform(xmlSource, result);
		        
		        return baos.toString();
	    		
	    	} catch(Exception e){e.printStackTrace();}
	    	
	    	return null;

	    }

}
